﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

namespace Cobit5.BIZ.Cobit
{
    public class Practice
    {
        #region Model
        private string _id;
        private string _processid;
        private string _practicename;
        private string _practicenamechn;
        private string _practicedesc;
        private string _practicedescchn;
        private string _temp;
        private string _comments;
        /// <summary>
        /// 
        /// </summary>
        public string id
        {
            set { _id = value; }
            get { return _id; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string processID
        {
            set { _processid = value; }
            get { return _processid; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string practiceName
        {
            set { _practicename = value; }
            get { return _practicename; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string practiceNameCHN
        {
            set { _practicenamechn = value; }
            get { return _practicenamechn; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string practiceDesc
        {
            set { _practicedesc = value; }
            get { return _practicedesc; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string practiceDescCHN
        {
            set { _practicedescchn = value; }
            get { return _practicedescchn; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string temp
        {
            set { _temp = value; }
            get { return _temp; }
        }

        /// <summary>
        /// 
        /// </summary>
        public string comments
        {
            set { _comments = value; }
            get { return _comments; }
        }

        #endregion Model

        #region PracticeFunction

        /// <summary>
        /// Initialize
        /// </summary>
        public Practice(string in_id)
        {
            if (Practice.ExistPractice(in_id) == 0)
                return;
            DAL.Practices optPractice = new DAL.Practices();
            Model.Practices myPractice = new Model.Practices();

            myPractice = optPractice.GetModel(in_id);
            this._id = myPractice.id;
            this._processid = myPractice.processID;
            this._practicename = myPractice.practiceName;
            this._practicenamechn = myPractice.practiceNameCHN;
            this._practicedesc = myPractice.practiceDesc;
            this._practicedescchn = myPractice.practiceDescCHN;
            this._temp = myPractice.temp;
        }

        /// <summary>
        /// Count the number of all practices
        /// </summary>
        public static int CountAllPractice()
        {
            DAL.Practices optPractice = new DAL.Practices();
            return optPractice.GetRecordCount("1=1");
        }

        /// <summary>
        /// List all the Practice in pages
        /// </summary>
        public static List<Practice> GetAllPractice(string in_orderBy, int in_startIndex, int in_endIndex)
        {
            DAL.Practices optPractice = new DAL.Practices();
            DataSet ds = optPractice.GetListByPage("processID<>'EMPTY'", in_orderBy, in_startIndex, in_endIndex);

            /*** Practice not exists ***/
            if (ds == null || ds.Tables[0].Rows.Count < 1)
                return null;

            List<Practice> returnPractices = new List<Practice>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Practice newPractice = new Practice(ds.Tables[0].Rows[i]["id"].ToString());
                returnPractices.Add(newPractice);
            }

            return returnPractices;
        }

        /// <summary>
        /// List all the Practice with no Process in pages
        /// </summary>
        public static List<Practice> GetAllPracticeWithNoProcess(string in_orderBy, int in_startIndex, int in_endIndex)
        {
            DAL.Practices optPractice = new DAL.Practices();
            DataSet ds = optPractice.GetListByPage("processID='EMPTY'", in_orderBy, in_startIndex, in_endIndex);

            /*** Practice not exists ***/
            if (ds == null || ds.Tables[0].Rows.Count < 1)
                return null;

            List<Practice> returnPractices = new List<Practice>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Practice newPractice = new Practice(ds.Tables[0].Rows[i]["id"].ToString());
                returnPractices.Add(newPractice);
            }

            return returnPractices;
        }

        /// <summary>
        /// Check if this Practice exist
        /// Return: 1.Exist
        ///         0.Not Exist
        /// </summary>
        public static int ExistPractice(string in_id)
        {
            DAL.Practices optPractice = new DAL.Practices();
            if (optPractice.Exists(in_id)) return 1;
            else return 0;
        }

        /// <summary>
        /// Create a new Practice
        /// Return  1.Success
        ///         2.Id already existed
        ///         3.DAL Error
        /// </summary>
        public static int CreatePractice(string in_id, string in_processId, string in_practiceName, string in_practiceNameCHN, string in_practiceDesc, string in_practiceDescCHN, string in_temp)
        {
            if (Practice.ExistPractice(in_id) == 1)
                return 2;

            Model.Practices myPractice = new Model.Practices();
            myPractice.id = in_id;
            myPractice.processID = in_processId;
            myPractice.practiceName = in_practiceName;
            myPractice.practiceNameCHN = in_practiceNameCHN;
            myPractice.practiceDesc = in_practiceDesc;
            myPractice.practiceDescCHN = in_practiceDescCHN;
            myPractice.temp = in_temp;

            DAL.Practices optPractice = new DAL.Practices();
            bool ok = optPractice.Add(myPractice);
            if (!ok) return 3;
            else return 1;
        }

        /// <summary>
        /// Update the Practice
        /// Return: 1.Success
        ///         2.DAL Error
        /// </summary>
        public int UpdatePractice()
        {
            Model.Practices myPractice = new Model.Practices();
            myPractice.id = this._id;
            myPractice.processID = this._processid;
            myPractice.practiceName = this._practicename;
            myPractice.practiceNameCHN = this._practicenamechn;
            myPractice.practiceDesc = this._practicedesc;
            myPractice.practiceDescCHN = this._practicedescchn;
            myPractice.temp = this._temp;

            DAL.Practices optPractice = new DAL.Practices();
            bool ok = optPractice.Update(myPractice);
            if (!ok) return 2;
            else return 1;
        }

        /// <summary>
        /// 
        /// </summary>
        public static int DeletePractice(string in_id)
        {
            return 0;
        }

        #endregion PracticeFunction

        #region Practice-InputOutput

        /// <summary>
        /// Get the number of Input that belongs to this Practice
        /// </summary>
        public int CountInputPractice()
        {
            DAL.InputOutput optInO = new DAL.InputOutput();
            return  optInO.GetRecordCount("practiceID='" + this._id + "' and io='i'");
        }

        /// <summary>
        /// Get the number of Output that belongs to this Practice
        /// </summary>
        public int CountOutputPractice()
        {
            DAL.InputOutput optInO = new DAL.InputOutput();
            return optInO.GetRecordCount("practiceID='" + this._id + "' and io='o'");
        }

        /// <summary>
        /// Get the list of Input Practices
        /// Parameters: 1.in_orderBy:"practiceID","_counter"
        /// </summary>
        public List<Practice> InputPractices(string in_orderBy, int in_startIndex, int in_endIndex)
        {
            DAL.InputOutput optInO = new DAL.InputOutput();
            DataSet ds = optInO.GetListByPage("practiceID='" + this._id + "' and io='i'", in_orderBy, in_startIndex, in_endIndex);
            if (ds == null || ds.Tables[0].Rows.Count < 1)
                return null;

            List<Practice> returnListPractice = new List<Practice>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Practice newPractice = new Practice(ds.Tables[0].Rows[i]["practiceID"].ToString());
                newPractice.comments = ds.Tables[0].Rows[i]["comments"].ToString();
                returnListPractice.Add(newPractice);
            }
            return returnListPractice;
        }

        /// <summary>
        /// Get the list of Output Practices
        /// Parameters: 1.in_orderBy:"practiceID","_counter"
        /// </summary>
        public List<Practice> OutputPractices(string in_orderBy, int in_startIndex, int in_endIndex)
        {
            DAL.InputOutput optInO = new DAL.InputOutput();
            DataSet ds = optInO.GetListByPage("practiceID='" + this._id + "' and io='o'", in_orderBy, in_startIndex, in_endIndex);
            if (ds == null || ds.Tables[0].Rows.Count < 1)
                return null;

            List<Practice> returnListPractice = new List<Practice>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Practice newPractice = new Practice(ds.Tables[0].Rows[i]["practiceID"].ToString());
                newPractice.comments = ds.Tables[0].Rows[i]["comments"].ToString();
                returnListPractice.Add(newPractice);
            }
            return returnListPractice;
        }

        /// <summary>
        /// Add an Input Practice to this Practice
        /// Parameters: 1.in_practiceID: practice id
        ///             2.in_comments: comments
        /// Return: 2.GoalProcess not exists
        ///         3.Error in DAL
        ///         1.Success
        ///         4.Has already existed
        /// </summary>
        public int AddInputPractice(string in_practiceID, string in_comments)
        {
            if (Practice.ExistPractice(in_practiceID) == 0)
                return 2;

            Model.InputOutput myInO = new Model.InputOutput();
            DAL.InputOutput optInO = new DAL.InputOutput();

            DataSet ds = optInO.GetList("practiceID='" + this._id + "' and io='i' and practiceID2='" + in_practiceID + "'");
            if (ds == null || ds.Tables[0].Rows.Count < 1)
            {
                myInO._counter = optInO.GetMaxId();
                myInO.practiceID = this._id;
                myInO.io = "i";
                myInO.practiceID2 = in_practiceID;
                myInO.comments = in_comments;

                bool ok = optInO.Add(myInO);
                if (!ok) return 3;
                else return 1;
            }
            else
                return 4;
        }

        /// <summary>
        /// Add an Output Practice to this Practice
        /// Parameters: 1.in_practiceID: practice id
        ///             2.in_comments: comments
        /// Return: 2.GoalProcess not exists
        ///         3.Error in DAL
        ///         1.Success
        ///         4.Has already existed
        /// </summary>
        public int AddOutputPractice(string in_practiceID, string in_comments)
        {
            if (Practice.ExistPractice(in_practiceID) == 0)
                return 2;

            Model.InputOutput myInO = new Model.InputOutput();
            DAL.InputOutput optInO = new DAL.InputOutput();

            DataSet ds = optInO.GetList("practiceID='" + this._id + "' and io='o' and practiceID2='" + in_practiceID + "'");
            if (ds == null || ds.Tables[0].Rows.Count < 1)
            {
                myInO._counter = optInO.GetMaxId();
                myInO.practiceID = this._id;
                myInO.io = "o";
                myInO.practiceID2 = in_practiceID;
                myInO.comments = in_comments;

                bool ok = optInO.Add(myInO);
                if (!ok) return 3;
                else return 1;
            }
            else
                return 4;
        }

        /// <summary>
        /// Remove an Input Practice from this Practice
        /// Parameters: 1.in_id: Practice id
        /// Return: 2.Practice not exists
        ///         3.Error in DAL
        ///         1.Success
        /// </summary>
        public int RemoveInputPractice(string in_practiceID)
        {
            if (Practice.ExistPractice(in_practiceID) == 0)
                return 2;

            DAL.InputOutput optInO = new DAL.InputOutput();
            DataSet ds = optInO.GetList("practiceID='" + this._id + "' and io='i' and practiceID2='" + in_practiceID + "'");

            bool ok = optInO.Delete(int.Parse(ds.Tables[0].Rows[0]["_counter"].ToString()));
            if (!ok) return 3;
            else return 1;
        }

        /// <summary>
        /// Remove an Output Practice from this Practice
        /// Parameters: 1.in_id: Practice id
        /// Return: 2.Practice not exists
        ///         3.Error in DAL
        ///         1.Success
        /// </summary>
        public int RemoveOutputPractice(string in_practiceID)
        {
            if (Practice.ExistPractice(in_practiceID) == 0)
                return 2;

            DAL.InputOutput optInO = new DAL.InputOutput();
            DataSet ds = optInO.GetList("practiceID='" + this._id + "' and io='o' and practiceID2='" + in_practiceID + "'");

            bool ok = optInO.Delete(int.Parse(ds.Tables[0].Rows[0]["_counter"].ToString()));
            if (!ok) return 3;
            else return 1;
        }

        #endregion Practice-InputOutput

        #region Practice-Activity

        /// <summary>
        /// Get the number of Activity that belongs to this Practice
        /// </summary>
        public int CountActivity()
        {
            DAL.Activities optActivity = new DAL.Activities();
            return optActivity.GetRecordCount("practiceID='" + this._id + "'");
        }

        /// <summary>
        /// Get the list of Activities
        /// Parameters: 1.in_orderBy:"sequence","activityOwner","activityReviewer","frequency"
        /// </summary>
        public List<Activity> Activities(string in_orderBy, int in_startIndex, int in_endIndex)
        {
            DAL.Activities optActivity = new DAL.Activities();
            DataSet ds = optActivity.GetListByPage("practiceID='" + this._id + "'", in_orderBy, in_startIndex, in_endIndex);
            if (ds == null || ds.Tables[0].Rows.Count < 1)
                return null;

            List<Activity> returnListActivity = new List<Activity>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Activity newActivity = new Activity(this._id, int.Parse(ds.Tables[0].Rows[i]["sequence"].ToString()));
                returnListActivity.Add(newActivity);
            }

            return returnListActivity;
        }

        /// <summary>
        /// Add an Activity to this Practice
        /// Parameters: 1.in_sequence: Activity's sequence ID
        /// Return:     2.Such Activity already existed
        ///             3.Error in DAL
        ///             1.Success
        /// </summary>
        public int AddActivity(int in_sequence, string in_activity, string in_activityCHN, string in_activityOwner, string in_activityReviewer, int in_frequency)
        {
            int ok = Activity.CreateActivity(this._id, in_sequence, in_activity, in_activityCHN, in_activityOwner, in_activityReviewer, in_frequency);
            return ok;
        }

        /// <summary>
        /// Remove an Activity from this Practice: add 1000 to Activity's sequence
        /// Return: 2.Such Activity doesn't exist
        ///         3.Error in Delete the old +1000 Activity
        ///         4.Error in Changing the Activity into +1000
        ///         1.Success
        /// </summary>
        public int RemoveActivity(int in_sequence)
        {
            return 0;

            //Remove 就是 Delete
            //同时删除Supporting Template及Time Alarm
        }

        #endregion Practice-Activity

        #region Practice-Supporting

        /// <summary>
        /// Get the number of Supporting to this Activity
        /// </summary>
        public int CountSupporting()
        {
            DAL.SupportingTemplate optSupporting = new DAL.SupportingTemplate();
            return optSupporting.GetRecordCount("practiceID='" + this._id + "'");
        }

        /// <summary>
        /// Get the list of Supportings
        /// Parameters: 1.in_orderBy:"supportingID","sequence","updatedTime","supportingDesigner","supportingOwner","supportingReviewer","frequency","workingHours"
        /// </summary>
        public List<Supporting> Supportings(string in_orderBy, int in_startIndex, int in_endIndex)
        {
            DAL.SupportingTemplate optSupporting = new DAL.SupportingTemplate();
            DataSet ds = optSupporting.GetListByPage("practiceID='" + this._id + "'", in_orderBy, in_startIndex, in_endIndex);
            if (ds == null || ds.Tables[0].Rows.Count < 1)
                return null;

            List<Supporting> returnSupportings = new List<Supporting>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Supporting newSupporting = new Supporting(ds.Tables[0].Rows[i]["supportingID"].ToString());
                returnSupportings.Add(newSupporting);
            }

            return returnSupportings;
        }

        #endregion Practice-Supporting

        #region Practice-TimeAlarm

        /// <summary>
        /// Get the number of TimeAlarm that belong to this Activity
        /// </summary>
        public int CountTimeAlarm()
        {
            DAL.TimeAlarm optTA = new DAL.TimeAlarm();
            return optTA.GetRecordCount("practiceID='" + this._id + "' ");
        }

        /// <summary>
        /// Get the list of TimeAlarm
        /// Parameters: 1.in_orderBy:"practiceID","sequence","alarmDeadline","alarmStatus"
        /// </summary>
        public List<TimeAlarm> TimeAlarms(string in_orderBy, int in_startIndex, int in_endIndex)
        {
            DAL.TimeAlarm optTA = new DAL.TimeAlarm();
            DataSet ds = optTA.GetListByPage("practiceID='" + this._id + "' ", in_orderBy, in_startIndex, in_endIndex);
            if (ds == null || ds.Tables[0].Rows.Count < 1)
                return null;

            List<TimeAlarm> returnTimeAlarms = new List<TimeAlarm>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Guid temp_id = new Guid(ds.Tables[0].Rows[i]["_id"].ToString());
                TimeAlarm newTimeAlarm = new TimeAlarm(temp_id);
                returnTimeAlarms.Add(newTimeAlarm);
            }

            return returnTimeAlarms;
        }

        #endregion Practice-TimeAlarm

        /*还欠缺PracticeFunction中的Delete*/
    }
}
